# !python3      # 使用工具:PyCharm
# 用户登录名:yyds_l
# 项目名称: python爬虫     文件名称:获取元素的父节点1
# 系统日期:2022/3/15      系统时间:1:26
# 2022/03/15 01:26

"""ok"""
from bs4 import BeautifulSoup
html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister" id="link2"><i>ios</i>Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""


def print_hi():
    # 异常捕捉
    try:
        soup = BeautifulSoup(html, "html.parser")
        # print(soup.name)
        tag = soup.select("#link2")[0]  # (列表返回)
        # 获取元素父节点   parent (其中根节点<html>的父节点是名称为[document]的节点，这个[document]节点的父节点是None)
        op = tag.parent
        # print(op)
        # 获取元素的子节点 children (包括element、text等类型的节点)
        po = tag.children
        # for x in po:
            # print(x)
        # 获取所有的子孙节点元素 descendants (包括element、text等类型的节点)
        tag_p = soup.find("p")
        for y in tag_p.descendants:
            print(y)
        # 循环遍历出父节点的父节点 直到无
        # while tag:
            # print(tag.name)
            # tag = tag.parent

        # 多线程调用
        print_hi_two()
        # print("i")
    except Exception as err:
        print(err)


def print_hi_two():
    # 异常捕捉
    try:
        print()
    except Exception as err:
        print(err)


if __name__ == '__main__':
    print_hi()
